﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kaskelot.Services.Model;

namespace Stockholm.SOA.Kaskelot.WCF.ServiceImplementation.Mapping
{
	public static class KursTillfalleExtensions
	{
		public static DataContracts.KursTillfalle ToContract(this KursTillfalle entity)
		{
			DataContracts.KursTillfalle b = new DataContracts.KursTillfalle();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static DataContracts.KursTillfalle ToMinimalContract(this KursTillfalle entity)
		{
			DataContracts.KursTillfalle b = new DataContracts.KursTillfalle();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static KursTillfalle ToEntity(this DataContracts.KursTillfalle b)
		{
			KursTillfalle entity = new KursTillfalle();

			CopyToEntity(b, entity);

			return entity;
		}

		public static void UpdateEntity(this KursTillfalle entity, DataContracts.KursTillfalle KursTillfalle)
		{
			CopyToEntity(KursTillfalle, entity);
		}

		private static void CopyFromEntity(KursTillfalle from, DataContracts.KursTillfalle to, bool minimal)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;

			to.Kursdatum = from.Kursdatum;
			to.KursId = from.KursId;

			to.KursNamn = from.Kurs.Beskrivning;
			to.LedigaPlatser = from.PersonKursTillfalle != null ? from.PersonKursTillfalle.Where(pkt => pkt.PersonId == null).ToArray().Length.ToString() : String.Empty;

			to.Starttid = from.Starttid;
			to.Sluttid = from.Sluttid;

			to.Sal = from.Sal;
			to.Adress = from.Adress;

			to.Kursansvarig = from.Kursansvarig;
			to.Lokal = from.Lokal;

			if (from.Kursansvarig.HasValue)
				to.KursansvarigNamn = from.Person.Fornamn + " " + from.Person.Efternamn;
		}

		private static void CopyToEntity(DataContracts.KursTillfalle from, KursTillfalle to)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;

			to.KursId = from.KursId;
			to.Kursdatum = from.Kursdatum;

			to.Starttid = from.Starttid;
			to.Sluttid = from.Sluttid;

			to.Sal = from.Sal;
			to.Adress = from.Adress;

			to.Kursansvarig = from.Kursansvarig;
			to.Lokal = from.Lokal;
		}
	}
}
